package cn.qinhao.pethome.shop.controller;

import cn.qinhao.pethome.annotation.PreAuthorize;
import cn.qinhao.pethome.basic.exception.BusinessException;
import cn.qinhao.pethome.basic.until.AjaxResult;
import cn.qinhao.pethome.basic.until.ExcelUtil;
import cn.qinhao.pethome.basic.until.PageList;
import cn.qinhao.pethome.shop.domain.Shop;
import cn.qinhao.pethome.shop.domain.ShopColumnchartVo;
import cn.qinhao.pethome.shop.domain.ShopPicEchartsVo;
import cn.qinhao.pethome.shop.domain.ShopVo;
import cn.qinhao.pethome.shop.query.ShopQuery;
import cn.qinhao.pethome.shop.service.IShopService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "店铺保存" , value="shop:save")
    @ApiOperation(value = "新增或修改操作" )
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me().setSuccess(true).setMessage("保存成功");
        } catch (BusinessException e) {//根据构造方法传什么进去就取什么出来，业务的错就再这报
            return AjaxResult .me() .setSuccess(false ).setMessage(e.getMessage());
        } catch (Exception e) {
            return AjaxResult .me() .setSuccess(false ).setMessage("保存失败");
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "店铺删除" , value="shop:delete")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me().setSuccess(true).setMessage("删除成功");
        } catch (Exception e) {
            return AjaxResult .me() .setSuccess(false ).setMessage("删除失败");
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.selectOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    @ApiOperation(value = "查询所有数据" )
    public List<Shop> list(){

        return shopService.selectAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @PreAuthorize(name = "店铺分页列表" , value="shop:list")
    @ApiOperation(value = "分页查询" )
    public PageList<Shop> queryPage(@RequestBody ShopQuery query)
    {
        return shopService.pageQuery(query);
    }

    //审核通过    VO 就专门用来返回数据 和 接收前端的数据,所以先去封装
    @PostMapping("/success")
    public AjaxResult success(@RequestBody ShopVo shopVo){
        System.out.println(shopVo.getId());
        try {
            shopService.success(shopVo);
            return AjaxResult.me().setSuccess(true).setMessage("操作成功");
        } catch (BusinessException e) {
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }  catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("网络繁忙");
        }
    }
    //驳回
    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody ShopVo shopVo) {
        try {
            shopService.reject(shopVo);
            return AjaxResult.me().setSuccess(true).setMessage("操作成功");
        } catch (BusinessException e) {
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("网络繁忙");
        }
    }
    //激活操作Activate
   @GetMapping("/activate/{id}")
    public String activate(@PathVariable("id") Long id) {

            shopService.activate(id);
           return "激活成功";
    }
    //导出表格全部数据
    @GetMapping("/exportExcel")
    public void export(HttpServletResponse response) {
        List<Shop> shops = shopService.selectAll();
        ExcelUtil.exportExcel(shops, "店铺审核数据", "表格1", Shop.class, "宠物店铺.xlsx", response);
    }
    //导入表格全部数据
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 1, 1, Shop.class);
        for (Shop shop : shops) {
            System.out.println(shop.toString());
        }
    }
    //饼图
    @GetMapping("/pieEcharts")
    public ShopPicEchartsVo pieEcharts(){
        return shopService.pieEcharts();
    }
    //柱状图
    @GetMapping("/chartColumn")
    public ShopColumnchartVo ColumnEcharts(){
        return shopService.ColumnEcharts();
    }
}
